#include <jni.h>
#include <string>
#include "YUVRotate.h"

extern "C"
JNIEXPORT jbyteArray JNICALL
Java_com_roy_yuvlib_YUVRotate_NV12RotateToNV21(JNIEnv *env, jobject thiz, jbyteArray src_y,
                                               jint src_stride_y, jbyteArray src_uv,
                                               jint src_stride_uv, jint width, jint height,
                                               jint degress) {
    jint w_x_h = width*height;

    unsigned char * p_rot = (unsigned char *)malloc(w_x_h*3/2);

    jbyte* pYBuff  = env->GetByteArrayElements(src_y,NULL);
    jbyte* pUVBuff  = env->GetByteArrayElements(src_uv,NULL);

    NV12ToI420Rotate((const unsigned char*)pYBuff, width, (const unsigned char*)pUVBuff, width,
                     p_rot, height,
                     p_rot + w_x_h, height / 2,
                     p_rot + w_x_h + w_x_h / 4, height / 2,
                     width, height,
                     static_cast<RotationMode>(degress));

    env->ReleaseByteArrayElements(src_y,pYBuff,0);
    env->ReleaseByteArrayElements(src_uv,pUVBuff,0);

    jbyteArray dst_nv21 = env->NewByteArray(w_x_h*3/2);
    jbyte* pDstNV21Buff  = env->GetByteArrayElements(dst_nv21,NULL);


    I420ToNV21(p_rot, height,
               p_rot + w_x_h, height / 2,
               p_rot+ w_x_h + w_x_h / 4, height / 2,
               reinterpret_cast<unsigned char *>(pDstNV21Buff), height,
               reinterpret_cast<unsigned char *>(pDstNV21Buff + w_x_h), height,
               height, width
               );

    free(p_rot);
    env->ReleaseByteArrayElements(dst_nv21,pDstNV21Buff,0);

    return dst_nv21;
}
extern "C"
JNIEXPORT jbyteArray JNICALL
Java_com_roy_yuvlib_YUVRotate_I420RotateToNV21(JNIEnv *env, jobject thiz, jbyteArray src_y,
                                               jint src_stride_y, jbyteArray src_u,
                                               jint src_stride_u, jbyteArray src_v,
                                               jint src_stride_v, jint width, jint height,
                                               jint degress) {
    jint w_x_h = width*height;

    jbyteArray dst_nv21 = env->NewByteArray(w_x_h*3/2);

    unsigned char * p_rot = (unsigned char *)malloc(w_x_h*3/2);
    jbyte* pYBuff  = env->GetByteArrayElements(src_y,NULL);
    jbyte* pUBuff  = env->GetByteArrayElements(src_u,NULL);
    jbyte* pVBuff  = env->GetByteArrayElements(src_v,NULL);

    I420Rotate(reinterpret_cast<const unsigned char *>(pYBuff), src_stride_y,
               reinterpret_cast<const unsigned char *>(pUBuff), src_stride_u,
               reinterpret_cast<const unsigned char *>(pVBuff), src_stride_v,
               p_rot, height,
               p_rot +w_x_h, height / 2,
               p_rot+ w_x_h + w_x_h / 4, height / 2,
               width, height, static_cast<RotationMode>(degress));

    env->ReleaseByteArrayElements(src_y,pYBuff,0);
    env->ReleaseByteArrayElements(src_u,pUBuff,0);
    env->ReleaseByteArrayElements(src_v,pVBuff,0);

    jbyte* pDstNV21Buff  = env->GetByteArrayElements(dst_nv21,NULL);

    I420ToNV21(p_rot, height,
               p_rot +w_x_h, height / 2,
               p_rot+ w_x_h + w_x_h / 4, height / 2,
               reinterpret_cast<unsigned char *>(pDstNV21Buff), height,
               reinterpret_cast<unsigned char *>(pDstNV21Buff + w_x_h), height,
               height, width
    );

    free(p_rot);
    env->ReleaseByteArrayElements(dst_nv21,pDstNV21Buff,0);

    return dst_nv21;
}